#!/bin/bash
OPENOCD_PHAN="/usr/share/openocd"   #openocd 安装目录
APP_ADDR="0x08000000"
APP_FILE="./rtthread.bin"
#APP_FILE="./固件/YNG-CJM_V1.2_1.0.0.bin"
BOOT_ADDR="0x08000000"
BOOT_FILE="./YNG-CJM_V1.2_boot_f1.bin"
echo "请先下载安装openocd和stlink"
echo "openocd安装说明 https://www.rt-thread.org/qa/thread-421278-1-1.html"
echo "stlink安装说明 https://blog.csdn.net/xiao2yizhizai/article/details/54579926"

if [ -z $1 ]; then
    openocd -f "$OPENOCD_PHAN/scripts/interface/stlink-v2.cfg" -f "$OPENOCD_PHAN/scripts/target/stm32f1x_stlink.cfg"
else
    if [ $1 == "-h" ];then
        echo "-h 帮助"
        echo "-app 下载APP程序"
        echo "-boot 下载boot"
    elif [ $1 == "-boot" ];then
        openocd -f "$OPENOCD_PHAN/scripts/interface/stlink-v2.cfg" -f "$OPENOCD_PHAN/scripts/target/stm32f1x_stlink.cfg" -c init -c "reset halt" -c "flash write_image erase $BOOT_FILE $BOOT_ADDR" -c "reset run" -c shutdown
        # st-flash  --reset write "$BOOT_FILE" "$BOOT_ADDR"
    elif [ $1 == "-app" ];then
        openocd -f "$OPENOCD_PHAN/scripts/interface/stlink-v2.cfg" -f "$OPENOCD_PHAN/scripts/target/stm32f1x_stlink.cfg" -c init -c "reset halt" -c "flash write_image erase $APP_FILE $APP_ADDR" -c "reset run" -c shutdown
        #st-flash  --reset write "$APP_FILE" $APP_ADDR
    elif [ $1 == "-reset" ];then
        openocd -f "$OPENOCD_PHAN/scripts/interface/stlink-v2.cfg" -f "$OPENOCD_PHAN/scripts/target/stm32f1x_stlink.cfg" -c init -c "reset run" -c shutdown
    fi
fi


